/*
 * 文 件 名:  BitmapUtil.java
 * 版    权:  Shenzhen Coship Electronics Co.,Ltd. Copyright YYYY-YYYY,  All rights reserved
 * 描    述:  <描述>
 * 修 改 人:  905797
 * 修改时间:  2013-3-21
 * 跟踪单号:  <跟踪单号>
 * 修改单号:  <修改单号>
 * 修改内容:  <修改内容>
 */

package com.zhongketech.sunflower.utils;

import java.io.File;
import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory.Options;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.Log;
import android.view.View;

/**
 * Bitmap工具类
 */
@SuppressLint("NewApi")
public class BitmapUtil {
    private static String TAG = "BitmapUtil";

    public static int getBitmapSize(Bitmap bitmap) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1) {
            return bitmap.getByteCount();
        }
        return bitmap.getRowBytes() * bitmap.getHeight();
    }

    /**
     * @author 906707 将图片drawable按照h高度裁剪，返回裁剪后的图片
     * @param drawable
     */
    public static Bitmap createReflectedByCreateImages(Bitmap bitmap, int h) {

        if (bitmap == null) {
            return null;
        }

        int width = bitmap.getWidth();
        int height = bitmap.getHeight();

        Matrix matrix = new Matrix();
        matrix.preScale(1, -1);
        Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, height - h,
                width, h, matrix, false);

        return reflectionImage;

    }

    /**
     * @param sourceImg 传入的图片
     * @param number 0-100(0为完全透明，100为不透明)
     * @return Bitmap 处理后的图片
     */
    public static Bitmap setAlpha(Bitmap sourceImg, int number) {
        if (sourceImg == null) {
            return null;
        }
        int[] argb = new int[sourceImg.getWidth() * sourceImg.getHeight()];
        sourceImg.getPixels(argb, 0, sourceImg.getWidth(), 0, 0,
                sourceImg.getWidth(), sourceImg.getHeight());
        number = number * 255 / 100;
        double round = (double) number / (double) (argb.length);
        for (int i = 0; i < argb.length; i++) {
            if (number - i * round > 10) {
                argb[i] = ((int) (number - i * round) << 24)
                        | (argb[i] & 0x00FFFFFF);
                continue;
            } else {
                argb[i] = (10 << 24) | (argb[i] & 0x00FFFFFF);
                continue;
            }

        }
        sourceImg = Bitmap.createBitmap(argb, sourceImg.getWidth(),
                sourceImg.getHeight(), Config.ARGB_8888);

        return sourceImg;
    }

    /**
     * 根据图片路径pathName，获取width*heigth大小的图片，然后将该图片截取为Portal界面需要的倒影图片
     * 
     * @param pathName
     * @param width
     * @param height
     */
    public static Drawable createReflectBitmapByPath(String pathName,
            int width, int height) {
        final File file = new File(pathName);
        if (file.exists()) {
            Bitmap bitmap = ImageUtil.decodeSampledBitmapFromFile(pathName,
                    width, height);

            if (bitmap == null) {
                return null;
            }

            Drawable drawable = new BitmapDrawable(BitmapUtil.setAlpha(
                    BitmapUtil.createReflectedByCreateImages(bitmap, 75), 45));
            return drawable;
        } else {
            return null;
        }
    }

    public static boolean setReflectImg(View convertView, int viewId) {
        if (convertView == null) {
            return false;
        }
        String path = GetBitmap.getInstance().getBitmapData(viewId);
        if (path != null) {
            Drawable drawable = BitmapUtil.createReflectBitmapByPath(path, 544,
                    344);
            if (drawable != null) {
                convertView.setBackgroundDrawable(drawable);
                return true;
            }
        }
        return false;
    }

    /**
     * 缩放图片
     * 
     * @param img
     * @param newWidth
     * @param newHeight
     */
    public static Bitmap zoomImg(String img, int newWidth, int newHeight) {
        // 图片源
        Bitmap bm = BitmapFactory.decodeFile(img);
        if (null != bm) {
            return zoomImg(bm, newWidth, newHeight);
        }
        return null;
    }

    /**
     * 缩放图片
     * 
     * @param img
     * @param newWidth
     * @param newHeight
     */
    public static Bitmap zoomImg(Bitmap bm, int newWidth, int newHeight) {
        if (null == bm) {
            return null;
        }
        // 获得图片的宽高
        int width = bm.getWidth();
        int height = bm.getHeight();
        // 计算缩放比例
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;
        // 取得想要缩放的matrix参数
        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);
        // 得到新的图片
        Bitmap newbm = Bitmap.createBitmap(bm, 0, 0, width, height, matrix,
                true);
        return newbm;
    }

    public static Bitmap getBitmapByPath(String path, Options mOptions) {
        /*
         * BitmapFactory.Options mOptions = new BitmapFactory.Options();
         * mOptions.inDither = false; mOptions.inPreferredConfig =
         * Bitmap.Config.ARGB_8888;
         */

        try {
            if (mOptions != null)
                return BitmapFactory.decodeFile(path, mOptions);
            return BitmapFactory.decodeFile(path);
        } catch (Exception e) {
            // TODO: handle exception
            Log.d(TAG, e.getMessage());
            // e.printStackTrace();
            return null;
        }
    }

    public static Bitmap toRoundCorner(Bitmap bitmap, int pixels) {
        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
                bitmap.getHeight(), Config.ARGB_8888);
        Canvas canvas = new Canvas(output);
        final int color = 0xff424242;

        final Paint paint = new Paint();

        final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());

        final RectF rectF = new RectF(rect);

        final float roundPx = pixels;

        paint.setAntiAlias(true);

        canvas.drawARGB(0, 0, 0, 0);

        paint.setColor(color);

        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));

        canvas.drawBitmap(bitmap, rect, rect, paint);

        return output;

    }

}
